﻿using System;
using System.Collections;
using System.Reflection;
using log4net;
using log4net.Config;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Collections.Generic;
using System.ServiceModel.Description;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


namespace HaiFang
{
    [Serializable]
    public class ProxyModuleSubTable
    {
        private static string PROCEDURE_QueryModuleSubTable = "QueryModuleSubTable";
        private static string PROCEDURE_CreateModuleSubTable = "CreateModuleSubTable";
        private static string PROCEDURE_InsertModuleSubTable = "InsertModuleSubTable";
        private static string PROCEDURE_QueryModuleSubTableAutoID = "QueryModuleSubTableAutoID";

        //@CurrUserID		int = null,						--管理员编号
        //@ModuleSubID	bigint,
        //@QueryString	NVARCHAR(4000),					--字段名是FieldType+AutoID,字段和数值之间用逗号分隔，字段与字段之间以分号分隔，示例：I3,10;D2,2010-01-01,2020-01-01;C1,合同完成;B4,1;
        //@PageFirst	 	tinyint=0,						--是否第一次查询(默认0:首次查询)
        //@PageRow		int=1,							--页面显示数量
        //@PageJump		int=1,							--跳转页数	
        //@RecordNum		int output,
        //@PageNum		int output
        public static ModuleSubTableInfo QueryModuleSubTable(int CurrUserID, int ModuleSubID, string QueryString, int PageFirst, int PageRow, int PageJump, out int RecordNum, out int PageNum)
        {
            RecordNum = 0;
            PageNum = 0;
            IList<ModuleSubTable> list = new List<ModuleSubTable>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryModuleSubTable;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add((new SqlParameter("@ModuleSubID", ModuleSubID)));
                    Parameters.Add(new SqlParameter("@QueryString", QueryString));
                    Parameters.Add(new SqlParameter("@PageFirst", PageFirst));
                    Parameters.Add(new SqlParameter("@PageRow", PageRow));
                    Parameters.Add(new SqlParameter("@PageJump", PageJump));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@RecordNum",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = RecordNum
                    });
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@PageNum",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = PageNum
                    });
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = ModuleSubTable.GetModuleSubTable(dt);
                    //list = helper.GetObjects<ModuleSubTable>(dt);
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@RecordNum")
                        {
                            RecordNum = helper.GetInt(sp.Value);
                        }
                        else if (sp.ParameterName == "@PageNum")
                        {
                            PageNum = helper.GetInt(sp.Value);
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            ModuleSubTableInfo info = new ModuleSubTableInfo();
            info.RecordNum = RecordNum;
            info.PageNum = PageNum;
            info.ModuleSubTable = list;
            return info;
        }
        //@CurrUserID			int = null,					--管理员编号
        //@ModuleSubID		bigint,
        //@ErrorMSG			int	output					--100：已经存在数据 200:没有字段 0：正确；其他：错误
        public static void CreateModuleSubTable(int CurrUserID, int ModuleSubID, out int ErrorMSG)
        {
            ErrorMSG = -1;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_CreateModuleSubTable;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@ModuleSubID", ModuleSubID));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@ErrorMSG",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = ErrorMSG
                    });
                    #endregion

                    #region 获取执行结果
                    helper.Execute(comandText, CommandType.StoredProcedure, Parameters);
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@ErrorMSG")
                        {
                            ErrorMSG = helper.GetInt(sp.Value);
                            break;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
        }
        //@CurrUserID			int = null,					--管理员编号
        //@UseFlag			tinyint,					--1:insert 2:update   3:delete
        //@AutoID				bigint=0,					--@UseFlag为1时传0，其它操作传编号值
        //@ModuleSubID		bigint,
        //@SqlString			NVARCHAR(4000),				--字段名是FieldType+AutoID,字段和数值之间用逗号分隔，字段与字段之间以分号分隔，示例：I3,10;D2,2010-01-01;C1,合同完成;
        //@ErrorMSG			int	output					--100：不存在该表 200:错误 0：正确；其他：错误	
        public static void InsertModuleSubTable(int CurrUserID,int UseFlag,int AutoID,int ModuleSubID, string SqlString, out int ErrorMSG)
        {
            ErrorMSG = -1;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_InsertModuleSubTable;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@UseFlag", UseFlag));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    Parameters.Add(new SqlParameter("@ModuleSubID", ModuleSubID));
                    Parameters.Add(new SqlParameter("@SqlString", (SqlString == null) ? string.Empty : SqlString));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@ErrorMSG",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = ErrorMSG
                    });
                    #endregion

                    #region 获取执行结果
                    helper.Execute(comandText, CommandType.StoredProcedure, Parameters);
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@ErrorMSG")
                        {
                            ErrorMSG = helper.GetInt(sp.Value);
                            break;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
        }
        //@CurrUserID		int = null,						--管理员编号
        //@ModuleSubID	bigint,
        //@AutoID			bigint
        public static ModuleSubTable QueryModuleSubTableAutoID(int CurrUserID, int ModuleSubID, int AutoID)
        {
            ModuleSubTable mst = null;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryModuleSubTableAutoID;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add((new SqlParameter("@ModuleSubID", ModuleSubID)));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    mst = ModuleSubTable.GetModuleSubTableByAutoID(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return mst;
        }
    }

    [Serializable]
    public class ModuleSubTable
    {
        public int DisplayID { get; set; }
        public int AutoID { get; set; }
        public DateTime CreateTime { get; set; }
        public Hashtable KeyValue { get; set; }
        public static IList<ModuleSubTable> GetModuleSubTable(DataTable dt)
        {
            IList<ModuleSubTable> list = new List<ModuleSubTable>();
            if (dt != null)
            {
                Hashtable ht = new Hashtable();
                foreach (DataColumn dc in dt.Columns)
                {
                    switch (dc.ColumnName)
                    {
                        case "DisplayID":
                        case "AutoID":
                        case "CreateTime":
                            {
                            }
                            break;
                        default:
                            {
                                if (!ht.ContainsKey(dc.ColumnName))
                                {
                                    ht.Add(dc.ColumnName, null);
                                }
                            }
                            break;
                    }
                }
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        ModuleSubTable mst = new ModuleSubTable();
                        mst.DisplayID = mst.GetInt(dr["DisplayID"]);
                        mst.AutoID = mst.GetInt(dr["AutoID"]);
                        mst.CreateTime = mst.GetDateTime(dr["CreateTime"]);
                        mst.KeyValue = ht.Clone() as Hashtable;
                        foreach (string key in ht.Keys)
                        {
                            mst.KeyValue[key] = dr[key];
                        }
                        list.Add(mst);
                    }
                }
            }
            return list;
        }
        public static ModuleSubTable GetModuleSubTableByAutoID(DataTable dt)
        {
            ModuleSubTable mst = null;
            if (dt != null)
            {
                Hashtable ht = new Hashtable();
                foreach (DataColumn dc in dt.Columns)
                {
                    switch (dc.ColumnName)
                    {
                        case "DisplayID":
                        case "AutoID":
                        case "CreateTime":
                            {
                            }
                            break;
                        default:
                            {
                                if (!ht.ContainsKey(dc.ColumnName))
                                {
                                    ht.Add(dc.ColumnName, null);
                                }
                            }
                            break;
                    }
                }
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        mst = new ModuleSubTable();
                        //mst.DisplayID = mst.GetInt(dr["DisplayID"]);
                        mst.AutoID = mst.GetInt(dr["AutoID"]);
                        mst.CreateTime = mst.GetDateTime(dr["CreateTime"]);
                        mst.KeyValue = ht.Clone() as Hashtable;
                        foreach (string key in ht.Keys)
                        {
                            mst.KeyValue[key] = dr[key];
                        }
                        break;
                    }
                }
            }
            return mst;
        }
        public string GetString(object dr)
        {
            string r = string.Empty;
            if (dr != null && dr != DBNull.Value)
            {
                r = dr.ToString();
            }
            return r;
        }
        public long GetLong(object dr)
        {
            long r = 0L;
            if (dr != null && dr != DBNull.Value)
            {
                long.TryParse(dr.ToString(), out r);
            }
            return r;
        }
        public int GetInt(object dr)
        {
            int r = 0;
            if (dr != null && dr != DBNull.Value)
            {
                int.TryParse(dr.ToString(), out r);
            }
            return r;
        }
        public DateTime GetDateTime(object dr)
        {
            DateTime dt;
            if (dr != null && dr != DBNull.Value)
            {
                if (DateTime.TryParse(dr.ToString(), out dt))
                {
                    return dt;
                }
            }
            return DateTime.MinValue;
        }
    }

    [Serializable]
    public class ModuleSubTableInfo
    {
        public int RecordNum { get; set; }
        public int PageNum { get; set; }
        public IList<ModuleSubTable> ModuleSubTable { get; set; }
    }

    [Serializable]
    public class ModuleSubTableRequest
    {
        public int ModuleSubID { get; set; }
        public string QueryString { get; set; }
        public int PageFirst { get; set; }
        public int PageRow { get; set; }
        public int PageJump { get; set; }
        public int RecordNum { get; set; }
        public int PageNum { get; set; }
    }

    [Serializable]
    public class ModuleSubTableInsert
    {
        public int ModuleSubID { get; set; }
        public string SqlString { get; set; }
        public int AutoID { get; set; }
    }

    [Serializable]
    public class ModuleSubTableSingleRequest
    {
        public int ModuleSubID { get; set; }
        public int AutoID { get; set; }
    }
}